package com.unknow.first.dapp.admin.account.job;

import com.unknow.first.dapp.admin.account.service.AddressWithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.cloud.core.redis.RedisUtil;
import org.cloud.scheduler.constants.MisfireEnum;
import org.cloud.scheduler.job.BaseQuartzJobBean;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.Assert;


/**
 * 扫描日志
 */
@Slf4j
public class Erc20WithdrawJob extends BaseQuartzJobBean {

//    harvest-private-key: "df5e935996920389eb28da2546a719def000155e836187d1d9f95f114fd0f67a"
//    gas-price: "2_000_000"
//    gas-limit: "500_000"


    final private String redisLockerName = "AddressAccount.Erc20WithdrawJob.locker";
    RedisUtil redisUtil;
    private AddressWithdrawService addressWithdrawService;

    public Erc20WithdrawJob(RedisUtil redisUtil, AddressWithdrawService addressWithdrawService) {
        this.redisUtil = redisUtil;
        this.addressWithdrawService = addressWithdrawService;
    }


    @Override
    protected void init() {
        this.jobName = "定时更新ERC20线上提现订单的状态";
        jobData.put("description", "定时更新ERC20线上提现订单的状态");
        this.jobTime = "0/10 * * * * ? ";
        setMisfire(MisfireEnum.CronScheduleMisfireEnum.MISFIRE_INSTRUCTION_DO_NOTHING);
    }

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Assert.isTrue(redisUtil != null, "redisutil 未获取到");
        if (!redisUtil.getLock(redisLockerName, 60 * 60 * 1000)) {
            return;
        }
        try {
            this.addressWithdrawService.processWithdrawStatusDone();
        } catch (Exception e) {
            log.error("", e);
        } finally {
            redisUtil.releaseLock(redisLockerName);
        }
    }
}